package com.banciyuan.bcywebview.base.helper.data;

import android.content.Context;

import com.banciyuan.bcywebview.biz.detail.atperson.AtPersonProperties;
import com.banciyuan.bcywebview.utils.constants.MyConstants;
import com.banciyuan.bcywebview.utils.db.DBHelper;
import com.banciyuan.bcywebview.utils.sp.SPConstant;
import com.banciyuan.bcywebview.utils.sp.SPHelper;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.umeng.message.PushAgent;
import com.umeng.message.UTrack;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import de.greenrobot.daoexample.model.User;

public class UserDataHelper {

    private static User user;

    public static final String COSER_ROLE = "Coser";
    public static final String DRAWER_ROLE = "绘师";
    public static final String WRITE_ROLE = "写手";
    public static final String PHOTO_ROLE = "摄影";
    public static final String USER_LOGIN = "userlogin";

    public static synchronized void saveInstance(Context context) {
        if (user == null) {
            init(context);
        }
    }

    public static synchronized User getInstance(Context context) {
        if (user == null) {
            user = new User();
            init(context);
        }
        return user;
    }

    public static synchronized User getInstance() {
        if (user == null) {
            user = new User();
        }
        return user;
    }

    public static synchronized Boolean Login(JSONObject json, final Context context) {
        final User user = UserDataHelper.getInstance();
        try {
            user.setUid(json.getString("uid"));
        } catch (JSONException e) {
            user.setUid("");
        }
        try {
            user.setUname(json.getString("uname"));
        } catch (JSONException e) {
            user.setUname("");
        }
        try {
            user.setAvatar(json.getString("avatar"));
        } catch (JSONException e) {
            user.setAvatar("");
        }
        try {
            user.setSex(json.getString("sex"));
        } catch (JSONException e) {
            user.setSex("");
        }
        try {
            user.setLocation(json.getString("location"));
        } catch (JSONException e) {
            user.setLocation("");
        }
        try {
            user.setSelf_intro(json.getString("self_intro"));
        } catch (JSONException e) {
            user.setSelf_intro("");
        }
        try {
            user.setCtime(json.getString("ctime"));
        } catch (JSONException e) {
            user.setCtime("");
        }
        try {
            user.setToken(json.getString("token"));
        } catch (JSONException e) {
            return false;
        }
        user.setIs_coser("false");
        user.setIs_dealer("false");
        user.setIs_illust("false");
        user.setIs_photor("false");
        user.setIs_special("false");
        user.setIs_writer("false");
        user.setType_number("0");
        try {
            JSONArray jArray = json.getJSONArray("roles");
            if (jArray.length() > 2) user.setType_number("3");
            user.setType_number(jArray.length() + "");
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject jTemp = (JSONObject) jArray.get(i);
                String type_name = jTemp.getString("type_name");
                if (StringUtil.notNullEqual(type_name, UserDataHelper.COSER_ROLE)) {
                    user.setIs_coser("true");
                    user.setCp_id(jTemp.getString("cp_id"));
                } else if (StringUtil.notNullEqual(type_name, UserDataHelper.DRAWER_ROLE)) {
                    user.setIs_illust("true");
                    user.setDp_id(jTemp.getString("dp_id"));
                } else if (StringUtil.notNullEqual(type_name, UserDataHelper.WRITE_ROLE)) {
                    user.setIs_writer("true");
                    user.setWp_id(jTemp.getString("wp_id"));
                } else if (StringUtil.notNullEqual(type_name, UserDataHelper.PHOTO_ROLE)) {
                    user.setIs_photor("true");
                    user.setCp_id(jTemp.getString("cp_id"));
                }
            }
        } catch (JSONException e) {
        }

        if (DBHelper.getInstance(context).getUser() != null) {
            DBHelper.getInstance(context).deleteUser();
            DBHelper.getInstance(context).addUser(user);
        } else DBHelper.getInstance(context).addUser(user);

        Login(context);
        return true;
    }

    private static void init(Context mContext) {
        try {
            ArrayList<User> ulist = DBHelper.getInstance(mContext).getUser();
            if (ulist != null && !ulist.isEmpty()) {
                user = ulist.get(0);
            } else {
                user = new User();
                init();
            }
        } catch (Exception e) {
            user = new User();
            init();
        }
    }

    private static void init() {
        user.setAvatar("");
        user.setCp_id("");
        user.setCtime("");
        user.setDp_id("");
        user.setEmail("");
        user.setLocation("");
        user.setSelf_intro("");
        user.setSex("");
        user.setToken("");
        user.setUname("");
        user.setUid("");
        user.setType_number("0");
        user.setIs_coser("false");
        user.setIs_dealer("false");
        user.setIs_illust("false");
        user.setIs_photor("false");
        user.setIs_special("false");
        user.setIs_writer("false");

    }

    public static void Logout(final Context context, ClearAliasCallBack clearAliasCallBack) throws Exception {
        if (context == null) return;
        String uid = UserDataHelper.getInstance(context).getUid();
        clearUmengAlias(context, uid, clearAliasCallBack);

    }

    public static void clearUmengAlias(final Context context, final String uid, final ClearAliasCallBack clearAliasCallBack) {
        PushAgent mPushAgent = PushAgent.getInstance(context);
        mPushAgent.removeAlias(uid, MyConstants.BCYUSER, new UTrack.ICallBack() {
            @Override
            public void onMessage(boolean b, String s) {
                user = new User();
                init();
                SPHelper.putBoolean(context, SPConstant.USERLOGIN, false);
                SPHelper.putBoolean(context, SPConstant.SETALIASSUCC, false);
                DBHelper.getInstance(context).deleteUser();
                AtPersonProperties.getInstance().clearPersonNames();
                clearAliasCallBack.doclear();
            }
        });
    }

    public static void Login(Context context) {
        if (context == null) return;
        SPHelper.putBoolean(context, SPConstant.USERLOGIN, true);
    }

    public static Boolean ifLogin(Context context) {
        if (context == null) return false;
        return SPHelper.getBoolean(context, SPConstant.USERLOGIN, false);
    }

    public interface ClearAliasCallBack {
        void doclear();
    }


}